package cn.itcast.nems.studentrecord.web.controller.b;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.itcast.ic.common.bean.Page;
import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.enumeration.NemsErrorBodyEnum;
import cn.itcast.nems.common.util.NumberUtil;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.journal.dto.JournalClazzDTO;
import cn.itcast.nems.journal.dto.JournalRepeatModel;
import cn.itcast.nems.journal.service.JournalRepeatService;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import cn.itcast.nems.studentrecord.bo.JournalClazzModel;
import cn.itcast.nems.studentrecord.bo.SearchClazzQuery;
import cn.itcast.nems.studentrecord.dao.dataobject.JournalRepeatDO;
import cn.itcast.nems.studentrecord.dto.repeat.JournalRepeatDTO;
import cn.itcast.nems.studentrecord.web.vo.form.JournalRepeatEditVO;
import cn.itcast.nems.studentrecord.web.vo.query.JournalRepeatPageQueryVO;
import cn.itcast.nems.studentrecord.web.vo.query.JournalRepeatVO;
import cn.itcast.nems.studentrecord.web.vo.response.JournalRepeatPageVO;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 重读凭单 前端控制器
 * </p>
 *
 * @author 刘曙
 * @since 2023-12-12
 */
@Controller
@RequiredArgsConstructor
@RequestMapping("/b/repeat")
@Slf4j
public class JournalRepeatController {
    private final JournalRepeatService journalRepeatService;




    /**
     * 查询分页条件班级名称
     *
     * @param queryParam 分页参数
     * @return 班级列表(包含字段：班级ID、班级名称)
     */
    @PostMapping("/search/clazz")
    public ResponseEntity<Page<JournalClazzDTO>> findClazzList4PageSearch(@RequestBody BasePageRequest<SearchClazzQuery> queryParam) {
        queryParam.convert(50);
        JournalClazzModel clazzModel = CustomBeanUtil.copyProperties(queryParam.getCondition(), new JournalClazzModel());
        PageQueryModel<JournalClazzModel> queryModel = new PageQueryModel<>(queryParam.getPageNumber(), queryParam.getPageSize(), clazzModel);
        Page<JournalClazzDTO> pageData = journalRepeatService.findClazzList4PageSearch(queryModel);
        return ResponseEntity.ok(pageData);
    }

    /**
     * <p>
     * 重读凭单分页查询
     * </p>
     *
     * @param queryParam 请求参数
     * @return 分页数据
     * @author zhouwei
     * @since 2023/12/14 11:48 AM
     **/
    @PostMapping(value = {"/page/list","/page/list/my_commit","/page/list/wait_approval","/page/list/approved","/page/list/all"})
    public ResponseEntity<Page<JournalRepeatPageVO>> pageList(@RequestBody BasePageRequest<JournalRepeatPageQueryVO> queryParam) {
        queryParam.convert();
        JournalRepeatModel repeatModel  = CustomBeanUtil.copyProperties(queryParam.getCondition(), new JournalRepeatModel()) ;
        PageQueryModel<JournalRepeatModel> queryModel =new PageQueryModel<>(queryParam.getPageNumber(),  queryParam.getPageSize(),repeatModel) ;
        IPage<JournalRepeatDO> pageData = journalRepeatService.findPageList(queryModel) ;
        List<JournalRepeatPageVO> list = CollectionUtil.isEmpty(pageData.getRecords()) ? null :
                pageData.getRecords().stream().map(this::transferToJournalRepeatPageVO).toList();
        return ResponseEntity.ok().body(Page.of(queryModel.getPageNumber(), queryModel.getPageSize(), Long.valueOf(pageData.getTotal()).intValue(), list)) ;
    }

    @GetMapping(value = "/{id}")
    public ResponseEntity<JournalRepeatVO> getById(@PathVariable("id") String id) {
        log.info("【重读凭单查询详情】请求参数 ID：{}", id);
        JournalRepeatDTO repeatDTO =  journalRepeatService.findById(id) ;
        JournalRepeatVO repeatVO =   CustomBeanUtil.copyProperties(repeatDTO , new JournalRepeatVO()) ;
        repeatVO.setAmount(NumberUtil.convertToYuan(repeatDTO.getAmount()));
        repeatVO.setTargetClazzPrice(NumberUtil.convertToYuan(repeatDTO.getTargetClazzPrice()));
       return ResponseEntity.ok().body(repeatVO);
    }

    @PutMapping
    public ResponseEntity<String> create(@RequestBody JournalRepeatEditVO editVO) {
        JournalRepeatDTO repeatDTO = this.transferToJournalRenewalDTO(editVO) ;
       return ResponseEntity.ok().body(journalRepeatService.create(repeatDTO));
    }

    @DeleteMapping(value = "/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") String id) {
        journalRepeatService.deleteById(id);
        return ResponseEntity.ok().build();
    }
    /**
     * 提交重读凭单
     * @param  editVO 表单对象
     * @return 是否成功
     **/
    @PutMapping("/flow")
    public ResponseEntity<String> flow(@RequestBody @Validated JournalRepeatEditVO editVO) {
        log.info("[提交重读凭单草稿] 请求参数: {}" , JSON.toJSONString(editVO ));
        this.validateFlowRequestParam(editVO);
        JournalRepeatDTO repeatDTO = this.transferToJournalRenewalDTO(editVO) ;
        return ResponseEntity.ok(journalRepeatService.flow(repeatDTO));
    }

    /**
     * 提交
     * @param  id  升级凭单ID
     * @return 是否成功
     **/
    @PutMapping("/flow/{id}")
    public ResponseEntity<?> flow(@PathVariable("id")String id) {
        log.info("[提交升级凭单] 请求参数: {}" , JSON.toJSONString(id ));
        journalRepeatService.flow(id) ;
        return ResponseEntity.ok(true);
    }

    private JournalRepeatPageVO transferToJournalRepeatPageVO(JournalRepeatDO repeatDO) {
        JournalRepeatPageVO pageVO = CustomBeanUtil.copyProperties(repeatDO, new JournalRepeatPageVO()) ;
        pageVO.setAmount(NumberUtil.convertToYuan(repeatDO.getAmount()));
        return pageVO ;
    }

    private JournalRepeatDTO transferToJournalRenewalDTO(JournalRepeatEditVO editVO) {
        JournalRepeatDTO repeatDTO = CustomBeanUtil.copyProperties(editVO,new JournalRepeatDTO()) ;
        repeatDTO.setAmount(NumberUtil.convertToCent(editVO.getAmount()));
        repeatDTO.setTargetClazzPrice(NumberUtil.convertToCent(editVO.getTargetClazzPrice()));
        return repeatDTO ;
    }
    private void validateFlowRequestParam(JournalRepeatEditVO editVO) {
        Assert.notNull(editVO, BizExceptionProducer.produce(NemsErrorBodyEnum.PARAM_EMPTY ,"请求参数")) ;
        // Assert.notNull(editVO.getId(), BizExceptionProducer.produce(NemsErrorBodyEnum.PARAM_EMPTY ,"重读凭单ID")) ;
    }
}
